<template>
	<view class="container">
		<view class="refund-status-bg1" v-if="shangpin.state==1">
			<view style="font-size: 32rpx;">待领取</view>
			<view style="width: 50%;display: flex;justify-content: space-between;font-size: 28rpx;align-items: center;">
				<view class="title" v-if="shangpin.state==1" @tap="contactMerchantFun(shangpin.id)">{{shangpin.state|refundStatusFilter}}</view>
				<view class="title1" v-if="shangpin.state==1" @tap="shanchu(shangpin.id)">删除</view>
				<!-- <view class="title" v-if="shangpin.state==2" >{{shangpin.state|refundStatusFilter}}</view> -->
			</view>
		</view>
		<view class="refund-status-bg" v-if="shangpin.state==2">
			<text>已领取</text>
		</view>
		<view class="order-content">
			<view class="shop-info">
				<!-- <image :src="shopInfo.logo" mode=""></image> -->
				<view class="info">
					<view>
						<text class="distance" style="margin-right: 8rpx;">{{shangpin.activityName}}</text>
						<text>|</text>
						<text class="distance" style="margin-left: 8rpx;">合计金额￥{{shangpin.money}}</text>
					</view>
					<view style="font-size: 24rpx;font-family: PingFang SC;font-weight: 500;color: #FF892E;" v-if="shangpin.state==1">{{shangpin.state|refundStatusFilter}}</view>
					<!-- <view class="title1" v-if="shangpin.state==1" @tap="shanchu(shangpin.id)">删除</view> -->
					<view style="font-size: 24rpx;font-family: PingFang SC;font-weight: 500;color: #A3A4A4;" v-if="shangpin.state==2" >{{shangpin.state|refundStatusFilter}}</view>
					
				</view>
			</view>
			<view class="goods-list">
				<view class="good-item" v-for="item in shangpin.itemModelList" :key="item.id" >
					<view class="image-box">
							<image :src="imageUrl + item.toolingImage" mode=""></image>
						<view class="refund-status" v-if="item.refundState > 0">{{item.refundState | refundStatusFilter }}</view>
					</view>
					<view class="info-box">
						<view class="inline good-info">
							<view class="title">{{item.name}}</view>
						</view>
						<view class="inline number">
							<view class="spec">{{item.norm}}</view>
							<view class="num">*{{item.goodsNum}}</view>
						</view>
						<view class="inline money">
							<view class="price">￥ <text class="money">{{item.money}}</text></view>
							<!-- <view class="real-price">实付<text class="point">￥</text><text class="money">{{ filter.priceFormat(item.money / 100)}}</text></view> -->
							<view class="refund-btn" v-if="item.refundState > 0" @tap="refundDetail(item.refundId)">退款详情</view>
						</view>
					</view>
				</view>
				<view class="good-item" v-for="item in orderInfo.selOrderGoodsList" :key="item.id" v-if="isShowNormalItem">
					<view class="image-box">
						<image :src="item.logUrl != '' ? item.logUrl.split(',')[0] : ''" mode=""></image>
						<view class="refund-status" v-if="item.refundState > 0">{{item.refundState | refundStatusFilter }}</view>
					</view>
					<view class="info-box">
						<view class="inline good-info">
							<view class="title">{{item.name}}</view>
						</view>
						<view class="inline number">
							<view class="spec">{{item.spec}}</view>
							<view class="num">*{{item.goodsNum}}</view>
						</view>
						<view class="inline money">
							<view class="price">￥ <text class="money">{{item.money}}</text></view>
							<view class="real-price">实付<text class="point">￥</text><text class="money">{{item.money}}</text></view>
							<view class="refund-btn" v-if="item.refundState > 0" @tap="refundDetail(item.refundId)">退款详情</view>
						</view>
					</view>
				</view>
				<view class="open-down" v-if="orderInfo.goodsCount>2 && isShowOpenDowm" @tap="openDowm">
					<text>展开</text>
					<text>({{shangpin.goodsNum}})</text>
					<image src="../../../static/img/down.png" mode="" style="width: 26rpx;height:18rpx;"></image>
				</view>
				
				<view class="order-money">
					<view class="inline">
						<text class="label">合计金额</text>
						<view class="value"><text class="point red">￥</text> <text class="money red">{{shangpin.money}}</text></view>
					</view>
					<view class="inline">
						<view class="label">订单号</view>
						<view class="value">{{ shangpin.orderNo}}</view>
					</view>
				</view>
				<view class="order-info">
					<view class="inline">
						<view class="label">下单时间 </view>
						<view class="value">{{shangpin.createdTime}}</view>
					</view>
					<view class="inline">
						<text class="label">所属采购活动</text>
						<view class="value">{{shangpin.activityName}}</view>
					</view>
					<view class="inline">
						<text class="label">领取时间</text>
						<view class="value">{{shangpin.receivedTime ? shangpin.receivedTime :''}}</view>
					</view>
				</view>
			</view>
		</view>
		<contact-merchant  :phoneTitle="telephoneTitle" :phone="contactMerchant" v-if="contactMerchantShow" @closerMerchantPhone="closerMerchantPhoneFun"></contact-merchant>
	</view>
</template>

<script>
	const app = getApp();
	import contactMerchant from "../../components/contact-merchant/index";
	var timer = null;
	export default {
		data() {
			return {
				shangpin:'',
				imageUrl: 'http://124.71.202.247:8080/image/',
				goodsList:[],
				orderId:'',
				orderInfo:{},
				shopInfo:{},
				takeGoodsDate:'',
				nowDate:'',
				countdown:'',
				contactMerchantShow:false,
				contactMerchant:'',
				telephoneTitle:'团长电话',
				selOrderGoodsList:[],
				isShowItem:false,
				isShowNormalItem:true,
				isShowOpenDowm:true
			};
		},

		components: {
			contactMerchant
		},
		filters:{
			refundReasonFilter(type) {
				switch (type) {
					case 1:
						return '未到货';
						break;
					case 2:
						return '质量问题';
						break;
					case 3:
						return '包装破损';
						break;
					case 4:
						return '三无';
						break;
				}
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function (options) {
			let that=this;
			// this.orderId = options.orderId;
			
			that.shangpin=JSON.parse(uni.getStorageSync('dingdansj')) ;
			console.log("that.shangpin",that.shangpin)
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function () {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function () {
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function () {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function () {
			clearTimeout(timer);
		},
		
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function () {
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function () {
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function (res) {
			if (res.from === 'button') {}
		
			return getApp().globalData.shareApp();
		},
		filters:{
			statusFilter(type) {
				switch (type) {
					case 0:
						return '待付款';
						break;
					case 1:
						return '待发货';
						break;
					case 2:
						return '已发货';
						break;
					case 3:
						return '待提货';
						break;
					case 4:
						return '已收货';
						break;
					case 5:
						return '已完成';
						break;
					case 6:
						return '超时交易关闭';
						break;
					case 9:
						return '已取消';
						break;
				}
			},
			refundStatusFilter(type) {
				switch (type) {
					case 0:
						return '无退款';
						break;
					case 1:
						return '确认领取';
						break;
					case 2:
						return '已领取';
						break;
					case 3:
						return '完成退款';
						break;
					case 9:
						return '拒绝';
						break;
				}
			},
		},
		methods: {
			getDetails(){
				uni.showLoading({
					title:'加载中'
				})
				businessSuperApi.getOrderDetail(this.orderId).then(res=>{
					uni.hideLoading();
					console.log('详情',res);
					if(res.status == "SUCCEED"){
						this.orderInfo = res.result;
						if(this.orderInfo.goodsCount>2){
							this.selOrderGoodsList=this.orderInfo.selOrderGoodsList.slice(0,1);
							this.isShowItem=true;
							this.isShowNormalItem=false;
						}else{
							this.selOrderGoodsList=[];
							this.isShowItem=false;
							this.isShowNormalItem=true
						}
						if(this.orderInfo.orderState == 1 || this.orderInfo.orderState == 2 || this.orderInfo.orderState == 3 ) { //待提货
							let today = new Date();
							today.setTime(today.getTime()+24*60*60*1000);
							this.takeGoodsDate = `${today.getMonth()+1}月${today.getDate()}日 `
						}
						this.getDistance();
						this.countdownFun();
					}
				})
			},
			//点击展开按钮
			openDowm(){
				var that=this;
				that.selOrderGoodsList=[];
				that.isShowItem=false;
				that.isShowNormalItem=true
				that.isShowOpenDowm=false;
			},
			
			// 确认提货
			takeItem(){
				uni.showModal({
					title: '提示',
					content: '确认提货？',
					confirmText: '确认',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							businessSuperApi.receiveOrder(this.orderInfo.id).then(res=>{
								if(res.status == "SUCCEED"){
									uni.showToast({
										title: '提交成功',
										icon: 'none',
										duration: 2000
									})
									this.getDetails();
									uni.navigateBack({
					delta: 1
				});
								}else{
									uni.showToast({
										title: res.errorMessage,
										icon: 'none',
										duration: 2000
									})
								}
							})
						}
					}
				});
			},
			copy(orderNo){
				uni.setClipboardData({
					data:orderNo,//要被复制的内容
					success:()=>{//复制成功的回调函数
					  uni.showToast({//提示
						title:"复制成功"
					  })
					}
				});
			},
			refundDetail(id){
				uni.navigateTo({
					url:`/pages/refundDetail/refundDetail?refundId=${id}`
				})
			},
			// 关闭打电话窗口
			closerMerchantPhoneFun: function() {
				this.contactMerchantShow = false;
			},
			// 确认已领取工装吗
			contactMerchantFun: function(phone) {
			
				let that = this;
					console.log("that.shangpin",that.shangpin)
						console.log("phone",phone)
				
				uni.showModal({
					title: '确认已领取工装吗？',
					// content: '',
					confirmText: '确认',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							// const requestData = {
							//   url: '/order/fetch/' + params,
							//   method: 'put'
							// };
							that.sendRequest({
								url :"/order/fetch/"+phone,
								method : "PUT",
								// data :phone,
								hideLoading : true,
								success:function (res) {
										uni.showToast({
											title: '提交成功',
											icon: 'none',
											duration: 2000
										})
										uni.navigateBack({
											delta: 1
										});
										that.order = [];
										that.pagination.page=1;
										that.getOrderList(that.currentActive);
									}
							})
				
						}
					}
				});
			},
			shanchu:function(phone){
					let that = this;
					console.log("that.shangpin",that.shangpin)
				uni.showModal({
					title: '提示',
					content: '确认删除？',
					confirmText: '确认',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							that.sendRequest({
								url :"/order/"+phone,
								method : "DELETE",
								success:function (res) {
								console.log('删除res=>',res)
								if(res.status == "SUCCEED"){
									uni.showToast({
										title: '删除成功',
										icon: 'none',
										duration: 2000
									})
									uni.navigateBack({
											delta: 1
										}); 
										that.$nextTick(function(){
											that.order = [];
											that.pagination.page=1;
											that.getOrderList(that.currentActive)
										})	
									
								}else{
									setTimeout(() => {
									uni.showToast({
										title: res.errorMessage,
										icon: 'none',
										duration: 4000
									})
									},500)
								}
							}
						})
					}
				}
				})
			},
			// 导航
			toNavigation(){
				let location = this.shopInfo.location;
				let lat,lon;
				if(location != '' || location != null){
					lat = location.split(',')[0]
					lon = location.split(',')[1]
				}
				
			},
		}
	};
</script>
<style>
	@import "./refundOrderDetail.css";
</style>